#ifndef CUSTOM_JUMP_FLOOD_INCLUDED
#define CUSTOM_JUMP_FLOOD_INCLUDED

#include "GenerateSdfCommon.hlsl"

// Uniform Parameter
uniform float JumpLength;                          // Current Cascade Level n
Texture2D _BlitTexture;
SamplerState sampler_BlitTexture;

float4 JumpFloodFragment(Vert2Frag Input) : SV_TARGET {
    float4 RenderResult = float4(0, 0, 0, MAX_LENGTH);
    
    for (int IndexY = -1; IndexY <= 1; IndexY++)
        for (int IndexX = -1; IndexX <= 1; IndexX++) {
            float2 SampleUV = Input.ScreenUV * _ScreenParams.xy + float2(JumpLength * IndexX, JumpLength * IndexY);
            
            if (SampleUV.x < 0.0 || SampleUV.x >= _ScreenParams.x || SampleUV.y < 0.0 || SampleUV.y >= _ScreenParams.y) continue;

            float4 SampleResult = _BlitTexture.Load(int3(SampleUV, 0));
            float Distance = distance(SampleResult.xy, Input.ScreenUV * _ScreenParams.xy);
            
            if (SampleResult.x > 0 && Distance < RenderResult.w) {
                RenderResult.xy = SampleResult.xy;
                RenderResult.w = Distance;
            }
        }
    
    // float4 RenderResult = _BlitTexture.Sample(sampler_BlitTexture, Input.ScreenUV);
    return RenderResult;
}
#endif